//
//  AddressPickerView.m
//  testUTF8
//
//  Created by rhcf_wujh on 16/7/14.
//  Copyright © 2016年 wjh. All rights reserved.
//

#import "AddressPickerView.h"
#import "Province.h"

@interface AddressPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic ,strong) UIView   * titleBackgroundView;/**< 标题栏背景*/
@property (nonatomic ,strong) UIButton * cancelBtn;/**< 取消按钮*/
@property (nonatomic, strong) UIButton * sureBtn;/**< 完成按钮*/

@property (nonatomic ,strong) UIPickerView   * addressPickerView;/**< 选择器*/

@property (nonatomic ,strong) NSMutableArray * pArr;/**< 地址选择器数据源,装省份模型,每个省份模型内包含城市模型*/

@property (nonatomic ,strong) NSDictionary   * dataDict;/**< 省市区数据源字典*/
@property (nonatomic ,strong) NSMutableArray * provincesArr;/**< 省份名称数组*/
@property (nonatomic ,strong) NSDictionary   * citysDict;/**< 所有城市的字典*/
@property (nonatomic ,strong) NSDictionary   * areasDict;/**< 所有地区的字典*/

@property (nonatomic, strong) UIView * backgroundView;
/// 是否显示区  显示传yes  不显示传no
@property (nonatomic, assign) BOOL isShowArea;



@end
@implementation AddressPickerView

- (instancetype)initWithFrame:(CGRect)frame isShow:(BOOL) isShow {
    self = [super initWithFrame:frame];
    if (self) {
        _isShowArea = isShow;
        //加载地址数据源
        [self loadAddressData];
        //加载标题栏
        [self loadTitle];
        //加载选择器
        [self loadPickerView];
        /// 加载不透明背景
        [self addBackGroundView];
    }
    return self;
}

- (instancetype)initWithShowQu:(BOOL)isShow {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250);
        _isShowArea = isShow;
        //加载地址数据源
        [self loadAddressData];
        //加载标题栏
        [self loadTitle];
        //加载选择器
        [self loadPickerView];
        /// 加载不透明背景
        [self addBackGroundView];
    }
    return self;
}

#define SELFSIZE self.bounds.size
static CGFloat const TITLEHEIGHT = 50.0;
static CGFloat const TITLEBUTTONWIDTH = 75.0;

- (UIView *)titleBackgroundView{
    if (!_titleBackgroundView) {
        _titleBackgroundView = [[UIView alloc]initWithFrame:
                                CGRectMake(0, 0, SELFSIZE.width, TITLEHEIGHT)];
        _titleBackgroundView.backgroundColor = [UIColor color_main_background_color];
    }
    return _titleBackgroundView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:
                      CGRectMake(0, 0, TITLEBUTTONWIDTH, TITLEHEIGHT)];
        [_cancelBtn setTitle:@"取消"
                    forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor whiteColor]
                         forState:UIControlStateNormal];
        [_cancelBtn addTarget:self
                       action:@selector(cancelBtnClicked)
             forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]initWithFrame:
                    CGRectMake(SELFSIZE.width - TITLEBUTTONWIDTH, 0, TITLEBUTTONWIDTH, TITLEHEIGHT)];
        [_sureBtn setTitle:@"完成"
                  forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateNormal];
        [_sureBtn addTarget:self
                     action:@selector(sureBtnClicked)
           forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
- (UIPickerView *)addressPickerView{
    if (!_addressPickerView) {
        _addressPickerView = [[UIPickerView alloc]initWithFrame:
                              CGRectMake(0, TITLEHEIGHT, SELFSIZE.width, 165)];
        _addressPickerView.backgroundColor = [UIColor color_main_background_color];
        _addressPickerView.delegate = self;
        _addressPickerView.dataSource = self;
    }
    return _addressPickerView;
}
- (UIView *) backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
        _backgroundView.backgroundColor = [UIColor blackColor];
        _backgroundView.alpha = 0;
    }
    return _backgroundView;
}

/**
 加载backView
 */
- (void) addBackGroundView {
    [[UIApplication sharedApplication].keyWindow addSubview:self.backgroundView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

#pragma mark - 加载标题栏
- (void)loadTitle{
    [self addSubview:self.titleBackgroundView];
    [self.titleBackgroundView addSubview:self.cancelBtn];
    [self.titleBackgroundView addSubview:self.sureBtn];
}

#pragma mark  加载PickerView
- (void)loadPickerView{
    [self addSubview:self.addressPickerView];
}

#pragma mark - 加载地址数据
- (void)loadAddressData{
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"address"
                                                          ofType:@"txt"];

    NSError  * error;
    NSString * str22 = [NSString stringWithContentsOfFile:filePath
                                                 encoding:NSUTF8StringEncoding
                                                    error:&error];
    
    if (error) {
        return;
    }
    
    _dataDict = [self dictionaryWithJsonString:str22];
    
    if (!_dataDict) {
        return;
    }
    
    _provincesArr = [_dataDict objectForKey:@"p"];
    _citysDict    = [_dataDict objectForKey:@"c"];
    _areasDict    = [_dataDict objectForKey:@"a"];

    _pArr         = [[NSMutableArray alloc] init];
    
    //省份模型数组加载各个省份模型
    for (int i = 0 ;i < _provincesArr.count; i++) {
        NSArray  * citys = [_citysDict objectForKey:_provincesArr[i]];
        Province * p     = [Province provinceWithName:_provincesArr[i]
                                               cities:citys];
        [_pArr addObject:p];
    }
    
    //各个省份模型加载各自的所有城市模型
    for (Province * p in _pArr) {
        NSMutableArray * areasArr = [[NSMutableArray alloc] init];
        for (NSString * cityName in p.cities) {
            NSString * cityKey = [NSString stringWithFormat:@"%@-%@",p.name,cityName];
            NSArray  * cityArr = [_areasDict objectForKey:cityKey];
            City     * city    = [City cityWithName:cityName areas:cityArr];
            [areasArr addObject:city];
        }
        p.cityModels = areasArr;
    }
}

#pragma mark - UIPickerDatasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.isShowArea ? 3 : 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
    
        if (0 == component) {
            return _pArr.count;
        }
        else if (1 == component){
            NSInteger selectProvince = [pickerView selectedRowInComponent:0];
            Province  * p            = _pArr[selectProvince];
            return p.cities.count;
        }
        else if (2 == component){
            NSInteger selectProvince = [pickerView selectedRowInComponent:0];
            NSInteger selectCity     = [pickerView selectedRowInComponent:1];
            Province  * p            = _pArr[selectProvince];
            if (selectCity > p.cityModels.count - 1) {
                return 0;
            }
            City * c = p.cityModels[selectCity];
            return c.areas.count;
        }
    return 0;
}

#pragma mark - UIPickerViewDelegate
#pragma mark 填充文字
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
        
        if (0 == component) {
            Province * p = _pArr[row];
            return p.name;
        }
        else if (1 == component) {
            Province * selectP = _pArr[[pickerView selectedRowInComponent:0]];
            if (row > selectP.cities.count - 1) {
                return nil;
            }
            return selectP.cities[row];
        }
        else if (2 == component) {
            NSInteger selectProvince = [pickerView selectedRowInComponent:0];
            NSInteger selectCity     = [pickerView selectedRowInComponent:1];
            Province  * p            = _pArr[selectProvince];
            if (selectCity > p.cityModels.count - 1) {
                return nil;
            }
            City * c = p.cityModels[selectCity];
            if (row > c.areas.count -1 ) {
                return nil;
            }
            return c.areas[row];
        }
    
    return nil;
}

#pragma mark pickerView被选中
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component{
    if (0 == component) {
        if (self.isShowArea) {
            NSInteger selectCity = [pickerView selectedRowInComponent:1];
            NSInteger selectArea = [pickerView selectedRowInComponent:2];
            [pickerView reloadComponent:1];
            [pickerView selectRow:selectCity inComponent:1 animated:YES];
            [pickerView reloadComponent:2];
            [pickerView selectRow:selectArea inComponent:2 animated:YES];
        } else {
            NSInteger selectCity = [pickerView selectedRowInComponent:1];
            [pickerView reloadComponent:1];
            [pickerView selectRow:selectCity inComponent:1 animated:YES];
        }
        
    }
    else if (1 == component){
        if (self.isShowArea) {
            NSInteger selectArea = [pickerView selectedRowInComponent:2];
            [pickerView reloadComponent:2];
            [pickerView selectRow:selectArea inComponent:2 animated:YES];
        } else {
            
        }

        
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row forComponent:(NSInteger)component
           reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.textColor = [UIColor color_text_white];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    
    pickerLabel.text = [self pickerView:pickerView
                            titleForRow:row
                           forComponent:component];
    return pickerLabel;
}

#pragma mark - 解析json

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData  * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError * err;
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


#pragma mark - AddressPickerViewDelegate
- (void)cancelBtnClicked{
    [self hide];
}

- (void)show{
    [self showOrHide:YES];
}

- (void)hide{
    [self showOrHide:NO];
}

- (void)showOrHide:(BOOL)isShow{
    WeakSelf(weSelf, self);
    CGFloat selfY = self.frame.origin.y;
    __block CGFloat selfkY = selfY;
    [UIView animateWithDuration:0.25 animations:^{
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:0.25];
        [UIView setAnimationDelegate:self];
        //改变它的frame的x,y的值
        
        if (isShow) {
            selfkY = [UIScreen mainScreen].bounds.size.height - SELFSIZE.height;
            weSelf.backgroundView.alpha = 0.5;
        }
        else {
            selfkY = [UIScreen mainScreen].bounds.size.height;
            weSelf.backgroundView.alpha = 0;
        }
        self.frame = CGRectMake(0,selfkY, self.bounds.size.width,SELFSIZE.height);

        [UIView commitAnimations];
    }];
}

- (void)sureBtnClicked{
    [self hide];
    if (self.sureMessage) {
        
    
    
        if (_isShowArea) {
            NSInteger selectProvince = [self.addressPickerView selectedRowInComponent:0];
            NSInteger selectCity     = [self.addressPickerView selectedRowInComponent:1];
            NSInteger selectArea     = [self.addressPickerView selectedRowInComponent:2];
            
            Province * p = _pArr[selectProvince];
            //解决省市同时滑动未结束时点击完成按钮的数组越界问题
            if (selectCity > p.cityModels.count - 1) {
                selectCity = p.cityModels.count - 1;
            }
            
            City * c = p.cityModels[selectCity];
            //解决省市区同时滑动未结束时点击完成按钮的数组越界问题
            if (selectArea > c.areas.count - 1) {
                selectArea = c.areas.count - 1;
            }
            
            self.sureMessage(p.name, c.cityName,c.areas[selectArea] );
        } else {
            NSInteger selectProvince = [self.addressPickerView selectedRowInComponent:0];
            NSInteger selectCity     = [self.addressPickerView selectedRowInComponent:1];
//            NSInteger selectArea     = [self.addressPickerView selectedRowInComponent:2];
            
            Province * p = _pArr[selectProvince];
            //解决省市同时滑动未结束时点击完成按钮的数组越界问题
            if (selectCity > p.cityModels.count - 1) {
                selectCity = p.cityModels.count - 1;
            }
            
            City * c = p.cityModels[selectCity];
            self.sureMessage(p.name, c.cityName, @"");
        }
    }
}





@end
